package com.wyl.mine.shang

import android.app.Activity
import android.content.Intent
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import com.alibaba.android.arouter.facade.annotation.Route
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.*
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.extend.toast
import com.wyl.libbase.utils.KLog
import com.wyl.mine.R
import com.wyl.mine.databinding.TiantongActivityBinding
import org.greenrobot.eventbus.EventBus

@Route(path = TiantongPage)
class TiantongActivity : BindingActivity<TiantongActivityBinding>() {
    val gold = ObservableField("0")
    val price = ObservableField("0")
    val index = ObservableInt(0)
    val type = ObservableInt(1) // 1支付宝 2微信

    override fun getLayoutId(): Int = R.layout.tiantong_activity

    override fun initView() {
        binding.vm = this

        gold.set(userData?.gold)

        binding.titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                CommonTitleBar.ACTION_RIGHT_TEXT ->
                    startActivity(Intent(this, TiantongListActivity::class.java))
                else -> {
                }
            }
        }
    }

    fun checkPrice(i: Int) {
        price.set(list[i]["money"])
        index.set(i)
    }

    fun checkType(i: Int) {
        type.set(i)
    }

    val list = ObservableArrayList<Map<String, String>>()

    override fun loadData() {
//        MineTiantong
        OkGo.post<BaseResponse<List<Map<String, String>>>>(MineTiantong)
            .converter(object : JsonConvert<BaseResponse<List<Map<String, String>>>>() {})
            .adapt(ObservableBody<BaseResponse<List<Map<String, String>>>>())
            .doOnSubscribe { showLoadingDialog() }
            .doFinally { dissmissLoadingDialog() }
            .map { it.data }
            .subscribe(
                {
                    list.addAll(it)
                    price.set(it[0]["money"])
                },
                { onError(it) }
            ).toDisposables(disposables)

    }

    fun commitString(i: String) = "立即支付${i}元"

    fun commit() {
        if (list.isEmpty()) return

        val number = list[index.get()]["number"]
        val money = list[index.get()]["money"]
        when (type.get()) {
            1 -> BeeCloudHelper.getInstance().payZhiFuBao(
                this, number, "0.1", ACacheHelper.id, result
            )
            2 -> BeeCloudHelper.getInstance().payWeiXin(
                this, number, "0.1", ACacheHelper.id, result
            )
        }
    }

    private val result = object : BeeCloudHelper.Result {
        override fun onSuccess() {
            runOnUiThread {
                toast("支付成功")
                setResult(RESULT_OK)
                onBackPressed()
            }
        }

        override fun onCancel() {

        }

        override fun onFailed(msg: String) {
            runOnUiThread {
                KLog.d(msg)
                toast("支付失败")
            }
        }

    }


}
